package runnerworm.core;

import java.awt.*;

/**
 * <p>Title: TunnelThread</p>
 * <p>Description: This will make the tunnel move.</p>
 * @author Guilherme Mauro Germoglio Barbosa
 */

public class TunnelThread
    extends Thread {

  /**
   * Where the tunnel will be drawn.
   */
  private Graphics graphics;
  /**
   * The tunnel.
   */
  private Tunnel tunnel;
  /**
   * How much the tunnel will move each time.
   */
  private int howMuch;

  /**
   * Constructor, receives the tunnel and how much will move.
   * @param tunnel The tunnel.
   * @param howMuch How much will move.
   * @param graphics Where the tunnel will be drawn.
   */
  public TunnelThread(Tunnel tunnel, int howMuch, Graphics graphics) {
    this.tunnel = tunnel;
    this.howMuch = howMuch;
    this.graphics = graphics;
  }

  /**
   * Runs the thread. Will move the tunnel.
   */
  public void run() {
    while (tunnel.isMoving()) {
      show();
      tunnel.translate(howMuch);
    }
  }

  /**
   * Shows the tunnel.
   */
  public synchronized void show() {
    tunnel.draw(graphics);
    try {
      sleep(24);
    }
    catch (InterruptedException e) {
      System.out.println("Oh fuck");
    }
    tunnel.erase(graphics);
  }


}